//
//  YLProductCollectionCell.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/23.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLProductCollectionCell.h"
#import "HYCircleProgressView.h"
#import <Masonry.h>
#import "YLGlobeHeader.h"
#import "HXTagsView.h"
#import "YLProductModel.h"
#import "HXTagAttribute.h"
@interface YLProductCollectionCell ()

// 产品名称
@property (weak, nonatomic) IBOutlet UILabel *productNameLabel;

// 进度条
@property (weak, nonatomic) HYCircleProgressView *progressView;
// 募集规模 年化收益率
@property (weak, nonatomic) IBOutlet UILabel *rateOfReturnLabel;
// 期限
@property (weak, nonatomic) IBOutlet UILabel *dayLabel;
// 燕尾标签底部
@property (weak, nonatomic) IBOutlet UIView *tagBaseView;
// 特性
@property (weak, nonatomic) IBOutlet UILabel *featuresLabel;
// 类型 ： 年化或者募集规模
@property (weak, nonatomic) IBOutlet UILabel *typeLabel;
// 很多标签的底部
@property (weak, nonatomic) IBOutlet UIView *tagView;
// 很多标签
@property (nonatomic, weak) HXTagsView *tagsView;
@end


@implementation YLProductCollectionCell


- (void)setModel:(YLProductModel *)model
{
    _model = model;
    
    _rateOfReturnLabel.attributedText = [self str:_model.annualizedYield strFont:[UIFont systemFontOfSize:28.0f] andAppendString:@"%" appendStrFont:[UIFont systemFontOfSize:18.0f]];
    
    _dayLabel.attributedText = [self str:[NSString stringWithFormat:@"%zd", _model.productTerm] strFont:[UIFont systemFontOfSize:28.0f] andAppendString:@"个月" appendStrFont:[UIFont systemFontOfSize:18.0f]];
    
    _productNameLabel.text = model.productName;
    if (_productNameLabel.text.length > 10) {
        _productNameLabel.text = [_productNameLabel.text substringToIndex:10];
        _productNameLabel.text = [NSString stringWithFormat:@"%@...", _productNameLabel.text];
    }

    _productNameLabel.text = [NSString stringWithFormat:@"%@(%.1f万起投)",_productNameLabel.text, model.leastMoney / 10000.0];

    [_productNameLabel sizeToFit];
    
    _featuresLabel.text = model.featuresText;
    
    // 有特性biao'shi
    if (model.featuresText.length == 0) {
        _tagBaseView.alpha = 0;
        _tagsView.frame = CGRectMake(0, 0, YLScreenW - CGRectGetMaxX(_productNameLabel.frame) - 5, 30);
    } else {
        _tagBaseView.alpha = 1;
        _tagsView.frame = CGRectMake(0, 0, YLScreenW - CGRectGetMaxX(_productNameLabel.frame) - 90, 30);
    }

    CGFloat progress = (model.maxLimit - model.indexLimit) / model.maxLimit;
    
    if (model.maxLimit == 0) {
        progress = 0;
    }
    
    [_progressView setProgress:progress animated:YES];
    
    {
        _tagsView.tags = _model.usableVouchers;
    }
    
   
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    // 创建进度条
    HYCircleProgressView *progress = [[HYCircleProgressView alloc] initWithFrame:CGRectMake(YLScreenW - 20 - 46 , 48, 46, 46)];
    progress.backgroundStrokeColor = [UIColor lightGrayColor];
    progress.progressStrokeColor = [UIColor colorWithRed:234.0/255.0 green:86.0/255.0 blue:57.0/255.0 alpha:1];
    _progressView = progress;
    [self addSubview:progress];
    
    _typeLabel.text = @"预期年化收益率";
    
    _tagBaseView.hidden = NO;
    

    // 单行不需要设置高度,内部根据初始化参数自动计算高度
    // 创建各种标签
    HXTagsView *tagsView = [[HXTagsView alloc] initWithFrame:CGRectMake(0, 1, 0, 0)];
    _tagsView = tagsView;
    tagsView.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    tagsView.isMultiSelect = NO;
    tagsView.backgroundColor = [UIColor clearColor];
    tagsView.tagAttribute.titleSize = 10;
    
    [self.tagView addSubview:tagsView];
}

// 所有控制设置为不可用的颜色
- (void)setUnableColor
{
    _rateOfReturnLabel.backgroundColor = _dayLabel.backgroundColor = YLColor(147, 147, 147);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
}



#pragma mark 根据2段文字生成富文本
- (NSAttributedString *)str:(NSString *)string strFont:(UIFont *)strFont andAppendString:(NSString *)appendString appendStrFont:(UIFont *)appendStrFont
{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:string attributes:@{NSFontAttributeName : strFont}];
    [str appendAttributedString:[[NSMutableAttributedString alloc] initWithString:appendString attributes:@{NSFontAttributeName: appendStrFont}]];
    return str;
}


+ (NSString *)cellID
{
    return @"YLProductCollectionCell";
}

@end
